package com.time.client.application.controller;

import com.google.common.base.Preconditions;
import com.time.auth.entity.Result;
import com.time.client.application.converter.CommentDTOConverter;
import com.time.client.application.dto.CommentDTO;
import com.time.client.common.entity.PageResult;
import com.time.client.domain.entity.CommentBO;
import com.time.client.domain.service.ClientCommentDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 评论客户端控制层类
 *
 * @author: HuangQi
 * @date: Created in 10:39 2025/11/3
 * @description: 评论客户端控制层
 */
@Slf4j
@RestController
@RequestMapping("/client/comment/")
public class CommentController {

    @Autowired
    private ClientCommentDomainService clientCommentDomainService;

    /**
     * 新增评论
     *
     * @param commentDTO 新增评论信息
     * @return 新增结果
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody CommentDTO commentDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Client.CommentController.add.dto:{}", commentDTO);
            }
            // 参数校验
            Preconditions.checkNotNull(commentDTO.getParentCommentId(), "评论的父id不能为空");
            Preconditions.checkNotNull(commentDTO.getContent(), "评论的内容不能为空");

            // 调用领域层
            CommentBO commentBO = CommentDTOConverter.INSTANCE.convertDTOToBO(commentDTO);
            Boolean addResult = clientCommentDomainService.add(commentBO);

            // 返回结果
            return Result.ok(addResult);
        } catch (Exception e) {
            // 异常处理
            log.error("Client.CommentController.add.error:{}", e.getMessage(), e);
            return Result.fail("客户端模块新增评论失败");
        }
    }

    /**
     * 分页查询所有评论
     *
     * @param commentDTO 分页查询所有评论信息
     * @return 分页评论信息结果
     */
    @PostMapping("queryAll")
    public Result<PageResult<CommentDTO>> queryPageComment(@RequestBody CommentDTO commentDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Client.CommentController.queryPageComment.dto: {}", commentDTO);
            }

            // 调用领域层
            CommentBO commentBO = CommentDTOConverter.INSTANCE.convertDTOToBO(commentDTO);
            PageResult<CommentBO> infoBOPageResult = clientCommentDomainService.queryPageComment(commentBO);

            // 返回结果
            PageResult<CommentDTO> infoDTOPageResult = CommentDTOConverter.INSTANCE.convertBOPageToDTOPage(infoBOPageResult);
            return Result.ok(infoDTOPageResult);
        } catch (Exception e) {
            // 异常处理
            log.error("Client.CommentController.queryPageComment.error:{}", e.getMessage(), e);
            return Result.fail("客户端模块条件分页获取评论失败");
        }
    }

}
